package itsm.isperp.framework.web.response.view;

import itsm.isperp.framework.domain.ExcelExportModel;
import itsm.isperp.framework.utils.ReflectHelper;
import itsm.isperp.framework.web.request.JqGridColModel;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.data.domain.Page;
import org.springframework.web.servlet.view.document.AbstractExcelView;

/**
 * 
 * 
 * @author lizx
 * @date 2013-2-19
 * @version V1.0
 */
public class ExcelView extends AbstractExcelView {

	public void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String excelName = model.get("filePath") == null ? "导出数据.xls" : model
				.get("filePath").toString();

		// 设置response方式,使执行此controller时候自动出现下载页面,而非直接使用excel打开
		response.setContentType("APPLICATION/OCTET-STREAM");
		response.setHeader("Content-Disposition", "attachment; filename="
				+ URLEncoder.encode(excelName, "UTF-8"));
		// 退出构造
		if (model.get("list") == null) {
			return;
		}

		Object result = model.get("list");
		List list = null;
		if (result instanceof List) {
			list = (List) result;
		} else if (result instanceof Page) {
			list = ((Page) result).getContent();
		}

		List<JqGridColModel> excelModels = (List<JqGridColModel>) model
				.get("columns");

		// 产生Excel表头
		HSSFSheet sheet = workbook.createSheet("result");
		HSSFRow header = sheet.createRow(0); // 第0行

		HSSFCellStyle style = workbook.createCellStyle();
		HSSFFont font = workbook.createFont();
		font.setColor(HSSFColor.BLUE.index);
		font.setFontHeightInPoints((short) 14);
		font.setBoldweight((short) 24);
		style.setFont(font);
		style.setFillBackgroundColor(HSSFColor.YELLOW.index);

		List<ExcelExportModel> excelModelsNew = new ArrayList<ExcelExportModel>();
		// 产生标题列
		int j = 0;
		for (ExcelExportModel m : excelModels) {

			HSSFCell cell = header.createCell(j);
			cell.setCellValue(m.getLabel());
			cell.setCellStyle(style);
			excelModelsNew.add(m);
			j++;

		}
		// 填充数据
		int rowNum = 1;
		for (Object obj : list) {

			HSSFRow row = sheet.createRow(rowNum++);
			int length = excelModelsNew.size();
			for (int i = 0; i < length; i++) {

				ExcelExportModel c = excelModelsNew.get(i);
				Object targetObj = null;
				if (obj instanceof Map) {

					int chatAt = c.getName().indexOf(".");
					if (chatAt >= 0) {
						String s = c.getName().substring(chatAt + 1);

						String[] sR = s.split(" AS ");
						if (sR.length == 2) {
							s = sR[1].trim();
						}

						targetObj = ((Map) obj).get(s);
					} else {
						targetObj = ((Map) obj).get(c.getName());
					}
				} else {
					targetObj = ReflectHelper.getValueByFieldName(obj,
							c.getName());
				}

				if (targetObj == null) {
					row.createCell(i).setCellValue(" ");
				} else {
					row.createCell(i).setCellValue(String.valueOf(targetObj));
				}
			}

		}

	}
}
